package com.hundsun.cprs.yyt.service.match.core.command;

import java.util.concurrent.Executor;

import com.alibaba.fastjson.JSON;
import com.hundsun.cprs.yyt.service.match.core.Matching;
import com.hundsun.cprs.yyt.service.match.domain.DefaultTradeMatch;
import com.hundsun.cprs.yyt.service.match.domain.TradeEntrust;
import com.hundsun.cprs.yyt.service.match.domain.TradeMatchDetail;

public class DelistingEntrustCommand extends BaseCommand {

    protected TradeEntrust entrust;
    protected TradeEntrust tradeEntrustOther; // 与此订单成交的订单
    // 摘单订单号
    protected Long entrustNo;

    public DelistingEntrustCommand(TradeEntrust entrust) {
        this.entrust = entrust;
    }

    @Override
    public void perform(Matching matching, Executor executor) {
        // 获取指定挂牌单ID，进行摘牌动作
        long entrustPrice = entrust.getEntrustPrice();
        long amount = entrust.getEntrustAmount();// 摘单量

        // 根据指定委托号查询挂牌单信息
        TradeEntrust bestEntrust = null;
        if (entrust.isBuy()) {
            bestEntrust = matching.getSellQueue().getOldestEntrust();
        } else {
            bestEntrust = matching.getBuyQueue().getOldestEntrust();
        }
        tradeEntrustOther = bestEntrust;
        // 挂牌单已成交？或不存在，则直接退单
        if (bestEntrust == null) {
            entrust.setEntrustStatus(TradeEntrust.EntrustStatusDisused);
            // ServiceLocate.getTradeMatchService().cancelEntrust(entrust);
            System.out.println("没有可匹配的卖单信息.....");
            return;
        }

        final DefaultTradeMatch match = new DefaultTradeMatch(matching.getTradeCenter().getStockCode());
        long bestEntrustAmount = bestEntrust.getSurplusEntrustAmount();// 挂牌单的剩余量

        long actualSuitAmount = amount <= bestEntrustAmount ? amount : bestEntrustAmount;

        // 摘牌 要么全部成交,要么不成交打回
        entrust.subEntrustAmount(actualSuitAmount);
        bestEntrust.subEntrustAmount(actualSuitAmount);// 大家都扣掉成交的量
        match.addMatchDetail(new TradeMatchDetail(entrust, actualSuitAmount, entrustPrice, true));
        match.addMatchDetail(new TradeMatchDetail(bestEntrust, actualSuitAmount, entrustPrice, true));
        if (bestEntrust.isEntrustAmountAllMatch()) {
            // 挂牌单全部成交，则移除
            if (entrust.isBuy()) {
                matching.getSellQueue().removeEntrust(bestEntrust);
            } else {
                matching.getBuyQueue().removeEntrust(bestEntrust);
            }
        }

        final boolean isAllMatch = entrust.isEntrustAmountAllMatch();
        if (!isAllMatch) {
            // 委托摘牌单未全部成交，则部撤
            entrust.setEntrustStatus(TradeEntrust.EntrustStatusPartCanceled);
        }

        // 下单有成交 // 得到撮合序号
        // match.setMatchSeq(matching.getQuotes().getTradeSequence());
        if (logger.isDebugEnabled()) {
            logger.debug("ContinuousAuction Match Sequence:" + match.getMatchSeq());
        }
        //match.calculate();

        executor.execute(new Runnable() {
            public void run() {
                // 保存交易数据
                
                System.out.println(JSON.toJSONString(match));
                // ServiceLocate.getTradeMatchService().updateAfterMatch(match);
                
                if (!isAllMatch) {
                    // ServiceLocate.getTradeMatchService().cancelEntrust(entrust);
                }
            }
        });
    }
}
